package com.chuanyang.driverh.mainstart;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainstart.fragment.HomePagerLeaderFragment;
import com.chuanyang.driverh.mainstart.fragment.MyOrderBillFragment;
import com.chuanyang.driverh.mainstart.fragment.PersonalCenterFragment;
import com.chuanyang.driverh.mainstart.fragment.RealtimeFragment;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.PermissoinUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.eventbus.ExitAppEvent;
import com.chuanyang.driverh.utils.widget.MyViewPager;
import com.igexin.sdk.PushManager;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;


public class MainActivityLeader extends BaseTitleActivity {
    private MyViewPager vp_home;
    private RadioGroup rg_main;
    private RadioButton rb_first;
    private FragmentPagerAdapter adapter;
    public ArrayList list;
    private TextView tv_emp_msg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPermission();
//        BaseApplication.getInstance().getDriverInfoFromNet();
        BaseApplication.getInstance().startLoc();
        BaseApplication.getInstance().setActivity(this);
    }


    @Override
    public int getContentResId() {
        return R.layout.activity_main_leader;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        vp_home = (MyViewPager) findViewById(R.id.vp_home);
        vp_home.setOffscreenPageLimit(4);//，比较费内存
        rg_main = (RadioGroup) findViewById(R.id.rg_main);
        rb_first = (RadioButton) findViewById(R.id.rb_first);
        tv_emp_msg = (TextView) findViewById(R.id.tv_emp_msg);
    }


    private boolean bindAlias() {
        if(ConstUtil.isDebugMode){
         return    PushManager.getInstance().bindAlias(activityContext,"test"+ SharedPrefUtil.getUserId());
//            LogUtils.d("aliasLeader:  ","test" + SharedPrefUtil.getUserId());
        }else{
            return    PushManager.getInstance().bindAlias(activityContext, SharedPrefUtil.getUserId());
        }
    }

    @Override
    protected void initData() {
        //如果绑定不成功，再绑定一次。
        if (! bindAlias()){
            bindAlias();
        }
        getTitleTextView().setText("首页");
        hideTitleBar();
        list = new ArrayList();
        list.add(new HomePagerLeaderFragment());
        list.add(new MyOrderBillFragment());
        list.add(new RealtimeFragment());
        list.add(new PersonalCenterFragment());
        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        vp_home.setAdapter(adapter);
        rb_first.setChecked(true);

    }


    @Override
    protected void initListener() {
        rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_first:
                        getTitleTextView().setText("首页");
                        vp_home.setCurrentItem(0);
                        hideTitleBar();
                        break;
                    case R.id.rb_second:
                        getTitleTextView().setText("订单");
                        vp_home.setCurrentItem(1);
                        showTitleBar();
                        break;
                    case R.id.rb_third:
                        getTitleTextView().setText("消息");
                        vp_home.setCurrentItem(2);
                        showTitleBar();
                        break;
                    case R.id.rb_forth:
                        getTitleTextView().setText("我的");
                        vp_home.setCurrentItem(3);
                        showTitleBar();
                        break;
                }
            }

        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        }
    }



    private void getPermission() {
        try {
            PermissoinUtil.checkAndRequestPermissions(activityContext, new String[]{
                            android.Manifest.permission.ACCESS_FINE_LOCATION,
                            android.Manifest.permission.ACCESS_COARSE_LOCATION,
                            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            android.Manifest.permission.CAMERA,
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int position) {
            return (Fragment) list.get(position);
        }

    }

    private long mExitTime;
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                PrintUtil.toast(activityContext, "再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
                EventBus.getDefault().post(new ExitAppEvent(true));
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public TextView getHasMessageCount() {
        return tv_emp_msg;
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(SharedPrefUtil.getPreferences("messagecount"+ SharedPrefUtil.getUserId(),0)==null)
        {
            tv_emp_msg.setVisibility(View.GONE);
        }
        else {
            int count=SharedPrefUtil.getPreferences("messagecount"+SharedPrefUtil.getUserId(),0);
            if(count>99)      {
                tv_emp_msg.setText("99+");

            }else if(count==0){
                tv_emp_msg.setVisibility(View.GONE);
            }
            else {
                tv_emp_msg.setText(SharedPrefUtil.getPreferences("messagecount"+SharedPrefUtil.getUserId(),0)+"");

            }


        }
    }
}
